package com.share.finance.risk.third.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.common.common.Page;
import com.share.common.utils.GroovyUtil;
import com.share.finance.risk.common.model.constant.SysConf;
import com.share.finance.risk.third.mapper.InterfaceConfigMapper;
import com.share.finance.risk.third.model.db.InterfaceConfig;
import com.share.finance.risk.third.model.vo.InterfacePageReq;
import com.share.finance.risk.third.service.IInterfaceConfigService;
import groovy.lang.GroovyObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class InterfaceConfigServiceImpl extends ServiceImpl<InterfaceConfigMapper, InterfaceConfig> implements IInterfaceConfigService {
    private Map<String, InterfaceConfig> intIdMap = new HashMap<>();
    private Map<String, GroovyObject> intIdGroovyMap = new HashMap<>();
    private Map<String, GroovyObject> feeGroovyMap = new HashMap<>();

    @Autowired
    private InterfaceConfigMapper interfaceConfigMapper;

    @Override
    public Object init() {
        Map<String, InterfaceConfig> tmpintIdMap = new HashMap<>();
        Map<String, GroovyObject> tmpIntIdGroovyMap = new HashMap<>();
        Map<String, GroovyObject> tmpFeeGroovyMap = new HashMap<>();
        List<InterfaceConfig> interfaceConfigs = interfaceConfigMapper.selectList(null);
        for (InterfaceConfig po : interfaceConfigs) {
            tmpintIdMap.put(po.getIntId().toString(), po);

            //http的groovy
            String httpGroovyScript = po.getHttpGroovyScript();
            if (StringUtils.isNotBlank(httpGroovyScript)) {
                try {
                    GroovyObject groovyObject = GroovyUtil.createGroovyObject(httpGroovyScript);
                    tmpIntIdGroovyMap.put(po.getIntId(), groovyObject);//加入到缓存
                } catch (Exception e) {
                    log.error("httpgroovy脚本初始化失败po={}", JSON.toJSONString(po));
                }
            }

            //fee的groovy
            String feeGroovyScript = po.getFeeGroovyScript();
            if (StringUtils.isNotBlank(feeGroovyScript)) {
                try {
                    GroovyObject groovyObject = GroovyUtil.createGroovyObject(feeGroovyScript);
                    tmpFeeGroovyMap.put(po.getIntId(), groovyObject);//加入到缓存
                } catch (Exception e) {
                    log.error("feegroovy脚本初始化失败po={}", JSON.toJSONString(po), e);
                }
            }
        }

        intIdMap = tmpintIdMap;
        intIdGroovyMap = tmpIntIdGroovyMap;
        feeGroovyMap = tmpFeeGroovyMap;

        return interfaceConfigs;
    }

    @Override
    public InterfaceConfig getInterfaceConfig(String intId) {
        return intIdMap.get(intId);
    }

    @Override
    public Object exeGroovyScript(String fieldId, String method, Object param) {
        GroovyObject groovyObject = intIdGroovyMap.get(fieldId);
        try {
            return groovyObject.invokeMethod(method, param);
        } catch (Exception e) {
            log.error("feegroovy脚本初始化失败param={},method={},groovy={}", param, method, groovyObject);
            return null;
        }
    }

    @Override
    public String calcFee(String intId, String param) {
        GroovyObject groovyObject = intIdGroovyMap.get(intId);
        if (groovyObject == null) {
            return null;
        } else {
            String feeStatus = null;
            try {
                Object o = groovyObject.invokeMethod(SysConf.GROOVY_METHOD, param);
                feeStatus = (o == null) ? null : o.toString();
            } catch (Exception e) {
                log.error("feegroovy脚本初始化失败param={},groovy={}", param, groovyObject.toString());
                feeStatus = "2";
            }
            return feeStatus;
        }
    }

    @Override
    public Map<String, String> mapChannelParam(String intId) {
        return new HashMap<>();
    }

    @Override
    public Page pageList(InterfacePageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<InterfaceConfig> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getIntName())) {
            queryWrapper.like(InterfaceConfig::getIntName, req.getIntName());
        }
        if (StringUtils.isNotBlank(req.getIntSource())) {
            queryWrapper.like(InterfaceConfig::getIntSource, req.getIntSource());
        }
        if (StringUtils.isNotBlank(req.getRedisKeys())) {
            queryWrapper.like(InterfaceConfig::getRedisKeys, req.getRedisKeys());
        }
        if (StringUtils.isNotBlank(req.getIntId())) {
            queryWrapper.like(InterfaceConfig::getIntId, req.getIntId());
        }
        if (StringUtils.isNotBlank(req.getIntUrl())) {
            queryWrapper.like(InterfaceConfig::getIntUrl, req.getIntUrl());
        }
        if (StringUtils.isNotBlank(req.getIntMethod())) {
            queryWrapper.like(InterfaceConfig::getIntMethod, req.getIntMethod());
        }
        List<InterfaceConfig> list = interfaceConfigMapper.selectList(queryWrapper);
        return new Page<>(list, new PageInfo<>(list).getTotal(), req);
    }
}
